package com.gzhryc.jdbc.log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.jdbc.utils.JsonTools;

public class LogAnalysisUtils {
	
	static Pattern pat = Pattern.compile("\\{\\{([^}]+)\\}\\}");
	
	public static String out(String msg,Object...objs) {
		if(StringUtils.isNotBlank(msg) && objs != null && objs.length > 0) {
			Matcher mat = pat.matcher(msg);
			int mat_start = 0;
			while (mat.find(mat_start)){
				String str = mat.group(1);
			    if(str.contains(":")) {
			    	String[] temp = str.split(":");
			    	int i = Integer.valueOf(temp[0]);
			    	if(i < objs.length) {
			    		Object obj = objs[i];
			    		if(obj != null) {
					    	if("json".equals(temp[1])) {
					    		msg = msg.replace("{{"+str+"}}", JsonTools.toJson(obj));
					    	}else {
						    	msg = msg.replace("{{"+str+"}}", obj.toString());
					    	}
			    		}else {
			    			msg = msg.replace("{{"+str+"}}", "NULL");
			    		}
			    	}
			    }else {
			    	int i = Integer.valueOf(str);
			    	if(i < objs.length) {
			    		Object obj = objs[i];
			    		if(obj != null) {
			    			msg = msg.replace("{{"+str+"}}", obj.toString());
			    		}else {
			    			msg = msg.replace("{{"+str+"}}", "NULL");
			    		}
			    	}
			    }
			    mat_start = mat.end();
			}
		}
		return msg;
	}
}
